package handin04;

import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * PrimeGeneratorView shows the results of PrimeGenerator
 * 
 * @author Peter Tolstrup Aagesen, ptaa@itu.dk
 *
 */

@SuppressWarnings("serial")
public class PrimeGeneratorView extends JFrame{
	
	/**
	 * PrimeGeneratorView constructor
	 */
	public PrimeGeneratorView()
	{
		//Set size, title and close button
		setSize(400,300);
		setTitle("PrimeGenerator");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		

		//Set to invisible
		setVisible(false);		
	}
	
	/**
	 * Prints a list of the prime numbers from 1 to number;
	 * @param number int
	 */
	public void nextPrime(int number)
	{
		//Creates a list with the primes
		ArrayList<Integer> primeList = PrimeGenerator.getPrimeList(number);
		
		//Create the text 
		String primeText = "\nPrimes from 1 to " + number + ":\n";
		
		//Loop through the list
		for(int i = 0; i < primeList.size(); i ++)
		{
			//Add number and break line
			primeText += Integer.toString(primeList.get(i)) + "\n";
		}

		//Create a textField
		JTextArea textField = new JTextArea(primeText);
		//Wrap the textField in a scrollPane
		JScrollPane scrollPane = new JScrollPane(textField);
		
		//add the scrollPane (textField) to the frame
		add(scrollPane);
		
		//Make the frame visible
		setVisible(true);
	}
	
	/**
	 * Prints if a number is a prime or not
	 * @param number int
	 */
	public void isPrime(int number)
	{
		//Create text
		String primeText = "\n" + number + " is ";
		
		//Test if the number is a prime
		if(PrimeGenerator.isPrime(number))
		{
			primeText += "a prime... yay!";
		} else {
			primeText += "not a prime.\n" + number + " is devisible by " + PrimeGenerator.getFirstFactor(number);
		}	

		//Create a textField
		JTextArea textField = new JTextArea(primeText);
		
		//Add the textField to the frame
		add(textField);
		
		//Set frame to visible
		setVisible(true);
	}
}
